
#ifndef GET_PROCESS_ID_FROM_NAME_H
#define GET_PROCESS_ID_FROM_NAME_H

#include <windows.h>
#include <TlHelp32.h>


class _Func
{
public:
	virtual ~_Func() {};
	virtual BOOL operator()(LPPROCESSENTRY32W pPe32) = 0;
};

class CompareFunc : public _Func
{
public:
	int m_nSessionID;
public:

	CompareFunc(int nSessionID) : m_nSessionID(nSessionID) {};
	virtual ~CompareFunc() {};
	virtual BOOL operator()(LPPROCESSENTRY32W pPe32);
};

class NoneCompareFunc : public _Func
{
public:
	virtual ~NoneCompareFunc() {};
	virtual BOOL operator()(LPPROCESSENTRY32W pPe32) 
	{ 
		return TRUE;
	}
};

int GetProcessIdFromName(char *pszProcessName, _Func& FnCompare = NoneCompareFunc());

int TerminateProcessFromName(char *pszProcessName);

int TerminateProcessFromNameInSession(char *pszProcessName, DWORD dwSessionID);

int GetCurrentLogonUserToken(PHANDLE pToken);

DWORD GetProcessSessionID(DWORD dwProcessID);

typedef struct _PROFILEINFO {  
	DWORD dwSize;  
	DWORD dwFlags;  
	LPTSTR lpUserName;  
	LPTSTR lpProfilePath;  
	LPTSTR lpDefaultPath;  
	LPTSTR lpServerName;  
	LPTSTR lpPolicyPath;  
	HANDLE hProfile;
} PROFILEINFO, *LPPROFILEINFO;

typedef BOOL (WINAPI *CreateEnvironmentBlockFunc)(LPVOID*, HANDLE, BOOL);
typedef BOOL (WINAPI *DestroyEnvironmentBlockFunc)(LPVOID);
typedef BOOL (WINAPI *LoadUserProfileFunc)(HANDLE, LPPROFILEINFO);
typedef BOOL (WINAPI *UnloadUserProfileFunc)(HANDLE, HANDLE);
typedef BOOL (WINAPI *GetTokenInformationFunc)(HANDLE, TOKEN_INFORMATION_CLASS, LPVOID, DWORD, PDWORD);
typedef BOOL (WINAPI *LookupAccountSidFunc)(LPCSTR, PSID, LPSTR, LPDWORD, LPSTR, LPDWORD, PSID_NAME_USE);

typedef struct tag_USER_ENVIRONMENT_FUNC_HANDLE
{
	HMODULE m_hUserenv;
	CreateEnvironmentBlockFunc m_pCreateEnvironmentBlockFunc;
	DestroyEnvironmentBlockFunc m_pDestroyEnvironmentBlockFunc;
	LoadUserProfileFunc m_pLoadUserProfileFunc;
	UnloadUserProfileFunc m_pUnloadUserProfileFunc;

	HMODULE m_hAdvapi32;
	GetTokenInformationFunc m_pGetTokenInformationFunc;
	LookupAccountSidFunc m_pLookupAccountSidFunc;

} USER_ENVIRONMENT_FUNC_HANDLE, *PUSER_ENVIRONMENT_FUNC_HANDLE;

int LoadUserEnvironmentFunc(PUSER_ENVIRONMENT_FUNC_HANDLE pUserenvHandle);
int CreateProcessAsCurrentLogonUserFromName(PUSER_ENVIRONMENT_FUNC_HANDLE pUserenvHandle, char *pszProcessFullNameCommand, HANDLE *phProcess = NULL);
int UnloadUserEnvironmentFunc(PUSER_ENVIRONMENT_FUNC_HANDLE pUserenvHandle);

#endif
